package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IDestinationStaticVORedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVORedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.cache.vo.UserStatisVO;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.flight.service.IFlightService;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import cn.wolfcode.luowowo.member.domain.UserAttention;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.server.IUserAttentionService;
import cn.wolfcode.luowowo.member.server.IUserInfoService;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("mine")
public class MineController {

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Reference
    private ITravelStatisVORedisService travelStatisVORedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private ITravelCommentService travelCommentService;

    @Reference
    private IDestinationStaticVORedisService destinationStaticVORedisService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IUserAttentionService userAttentionService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IFlightService flightService;

    @Reference
    private IScenicCommentService scenicCommentService;

    @Reference
    private IScenicService scenicService;


    //我的游记
    @RequestMapping("mytravelnotes")
    public Object mytravelnotes(Model model, @UserParm UserInfo userInfo, @ModelAttribute("qo") TravelQueryObject qo) {
        if (userInfo == null) {
            return new LogistException("请登录");
        }
        model.addAttribute("userInfo", userInfo);

        userInfo.setUserMessage(userInfoService.getUserMessage(userInfo.getId()));

        //我的所有游记
        qo.setPageSize(5);
        PageInfo<Travel> pageInfo = travelService.getByAuthorId(userInfo.getId(), qo);
        for (Travel travel : pageInfo.getList()) {
            TravelStatisVO travelStatisVo = travelStatisVORedisService.getTravelStatisVo(travel.getId());
            BeanUtil.copyProperties(travel, travelStatisVo);
            Long id = travel.getDest().getId();
            Destination country = destinationService.getCountry(id);
            travel.getDest().setParent(country);
        }

        //我的游记
        model.addAttribute("pageInfo", pageInfo);

        //最近访客
        List<UserInfo> vistors = userInfoService.queryByVistor(userInfo.getUserMessage().getVistorIds());
        model.addAttribute("vistors", vistors);

        //游记的总回复
        //游记的总阅读量
        UserStatisVO vo = travelStatisVORedisService.getUserStatisVo(userInfo.getId());
        model.addAttribute("vo", vo);
        //累计访问
        model.addAttribute("us", userInfo.getUserMessage());

        return "mine/mytravelnotes";
    }


    //我的窝
    @RequestMapping("/home")
    public String homepage(@UserParm UserInfo userInfo, Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        //共享用户信息
        model.addAttribute("userInfo", userInfo);
        //关注
        List<UserAttention> attentions = userAttentionService.selecttAttentionsByUId(userInfo.getId());
        model.addAttribute("attention1", attentions.size());
        //我的关注者的头像
        model.addAttribute("attentions", attentions);
        //粉丝
        int fans = userAttentionService.selectFansByUid(userInfo.getId());
        model.addAttribute("fans", fans);
        //蜂蜜
        Long honey = userAttentionService.selectHoneyById(userInfo.getId());
        model.addAttribute("honey", honey);

        model.addAttribute("userInfo", userInfo);

        //我的所有游记
        qo.setPageSize(5);
        PageInfo<Travel> pageInfo = travelService.getByAuthorId(userInfo.getId(), qo);
        List<Travel> list = pageInfo.getList();
        for (Travel travel : list) {
            TravelStatisVO travelStatisVo = travelStatisVORedisService.getTravelStatisVo(travel.getId());
            BeanUtil.copyProperties(travel, travelStatisVo);
            Destination dest = travel.getDest();
            dest.setParent(destinationService.getCountry(dest.getId()));
        }
        //我的游记
        model.addAttribute("pageInfo", pageInfo);


        //我的点评
        List<Map<String,Object>> scenicList = new ArrayList<>();
        List<ScenicComment> scenicComments = scenicCommentService.getByUserId(userInfo.getId());
        for (ScenicComment scenicComment : scenicComments) {
            Scenic scenic = scenicService.get(scenicComment.getScenicId());
            Map<String,Object> map = new HashMap<>();
            map.put("scenic", scenic);
            map.put("scenicComment", scenicComment);
            scenicList.add(map);
        }
        model.addAttribute("scenicList", scenicList);
        model.addAttribute("size", scenicList.size());
        return "mine/homepage";

    }

    //我的点评
    @RequestMapping("/review")
    public Object review(Model model, @UserParm UserInfo userInfo) {
        if (userInfo == null){
            return new LogistException("请先登录");
        }
        //共享用户信息
        model.addAttribute("userInfo", userInfo);


        //我的景点点评
        List<Map<String,Object>> scenicList = new ArrayList<>();
        List<ScenicComment> scenicComments = scenicCommentService.getByUserId(userInfo.getId());
        for (ScenicComment scenicComment : scenicComments) {
            Scenic scenic = scenicService.get(scenicComment.getScenicId());
            Map<String,Object> map = new HashMap<>();
            map.put("scenic", scenic);
            map.put("scenicComment", scenicComment);
            scenicList.add(map);
        }
        model.addAttribute("scenicList", scenicList);
        model.addAttribute("size", scenicList.size());
        return "mine/review";
    }

    //我的收藏
    @RequestMapping("travelcollection")
    public Object travelcollection(Model model, @UserParm UserInfo userInfo) {
        if (userInfo == null) {
            return new LogistException("请登录");
        }
        model.addAttribute("userInfo", userInfo);

        //攻略收藏
        List<Long> sids = strategyStatisVORedisService.getStrategyIdsByUserId(userInfo.getId());
        List<Map<String, Object>> strategyList = new ArrayList<>();
        StrategyCommentQuery strategyqo = new StrategyCommentQuery();
        for (Long sid : sids) {
            StrategyDetail strategyDetail = strategyDetailService.get(sid);
            StrategyStatisVO strategyStatisVo = strategyStatisVORedisService.getStrategyStatisVo(sid);
            BeanUtil.copyProperties(strategyDetail, strategyStatisVo);
            strategyqo.setDetailId(sid);
            Page page = strategyCommentService.query(strategyqo);
            StrategyComment strategyComment = null;
            if (page.getContent().size() > 0) {
                strategyComment = ((StrategyComment) page.getContent().get(0));
            } else {
                strategyComment = new StrategyComment();
            }
            Map<String, Object> map = new HashMap<>();
            map.put("strategy", strategyDetail);
            map.put("strategyComment", strategyComment);
            strategyList.add(map);
        }
        model.addAttribute("strategys", strategyList);


        //游记收藏
        List<Long> tids = travelStatisVORedisService.getTravelIdsByUserId(userInfo.getId());
        List<Map<String, Object>> travelList = new ArrayList<>();
        TravelCommentQuery travelqo = new TravelCommentQuery();
        for (Long tid : tids) {
            TravelStatisVO travelStatisVo = travelStatisVORedisService.getTravelStatisVo(tid);
            Travel travel = travelService.queryById(tid);
            BeanUtil.copyProperties(travel, travelStatisVo);
            travelqo.setTravelId(tid);
            Page page = travelCommentService.query(travelqo);
            TravelComment travelComment = null;
            if (page.getContent().size() > 0) {
                travelComment = ((TravelComment) page.getContent().get(0));
            } else {
                travelComment = new TravelComment();
            }
            Map<String, Object> map = new HashMap<>();
            map.put("travel", travel);
            map.put("travelComment", travelComment);
            travelList.add(map);
        }
        model.addAttribute("travels", travelList);

        //地点收藏
        List<Long> dids = destinationStaticVORedisService.getDestIdsByUserId(userInfo.getId());
        List<Destination> dests = new ArrayList<>();
        for (Long did : dids) {
            Destination destination = destinationService.getById(did);
            destination.setParent(destinationService.getCountry(did));
            dests.add(destination);
        }
        model.addAttribute("dests", dests);

        return "mine/travelcollection";
    }

    //删除游记收藏
    @RequestMapping("/deleteTravelFavor")
    @ResponseBody
    public Object deleteTravelFavor(@UserParm UserInfo userInfo, Long tid){
        if (userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        travelStatisVORedisService.deleteTravelFavor(userInfo.getId(),tid);

        return AjaxResult.SUCCESS;
    }

    //设置
    @RequestMapping("/setting")
    public String setting(Model model, @UserParm UserInfo userInfo) {
        //共享用户信息
        model.addAttribute("userInfo", userInfo);
        //共享该用户的黑名单信息
        List<UserInfo> breakUser = userInfoService.selectbreakUser(userInfo.getId());
        model.addAttribute("breakUser", breakUser);
        return "mine/setting";
    }

    //修改手机号: 验证手机信息
    @RequestMapping("/verityOldPhone")
    @ResponseBody
    public Object verityOldPhone(String phone, String verifyCode) {

        try {
            userInfoService.verityOldPhone(phone, verifyCode);
        } catch (LogistException e1) {
            e1.printStackTrace();
            return new AjaxResult(false, e1.getMessage());
        } catch (Exception e) {
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
        return AjaxResult.SUCCESS;
    }

    //修改手机号: 验证手机信息
    @RequestMapping("/verityNewPhone")
    @ResponseBody
    public Object verityNewPhone(String phone, String verifyCode, @UserParm UserInfo userInfo) {

        try {
            userInfoService.updatePhone(userInfo.getId(), phone, verifyCode);
            System.out.println("");
        } catch (LogistException le) {
            le.printStackTrace();
            return new AjaxResult(false, le.getMessage());
        } catch (Exception e) {
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
        return AjaxResult.SUCCESS.addData("修改成功,重新登录生效");
    }

    //注销账户
    @RequestMapping("/logout")
    public String logout(@UserParm UserInfo userInfo) {
        userInfoService.logout(userInfo.getId(), UserInfo.STATE_LOGOUT);
        return "redirect:/login.html";
    }

    //搜索用户
    @RequestMapping("/searchUser")
    @ResponseBody
    public Object searchUser(Model model, String name) {
        UserInfo user = userInfoService.searchUser(name);
        if (user == null) {
            return new AjaxResult(false, "出用户不存在");
        }
        return AjaxResult.SUCCESS.addData(user);
    }

    //添加黑名单
    @RequestMapping("addbreakuser")
    @ResponseBody
    public Object addbreakuser(@UserParm UserInfo userInfo, Long id){

        try {
            userInfoService.addbreakuser(id,userInfo.getId());
        } catch (LogistException le) {
            le.printStackTrace();
            return new AjaxResult(false,le.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
        return AjaxResult.SUCCESS.addData("拉黑成功!");
    }
}
